package com.bianmaba;

import com.bianmaba.commons.annotation.WrapResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
public class GlobalController {


//
//    @RequestMapping(value = "calculate")
//    @JSON
//    public Object calculate() {
//        URLClassLoader urlClassLoader = null;
//        InputStreamReader reader = null;
//        try {
//            urlClassLoader = new URLClassLoader(new URL[]{new URL("file:E:\\workspaces\\wmp-algorithms\\out\\artifacts\\java_code_sum_jar\\java-code-sum.jar")});
//            InputStream is = urlClassLoader.getResourceAsStream("info.properties");
//            reader = new InputStreamReader(is, Charset.forName("utf-8"));
//            Properties properties = new Properties();
//            properties.load(reader);
//            String description = properties.getProperty("description");
//            String algorithms = properties.getProperty("algorithms");
//            System.out.println(description);
//            System.out.println(algorithms);
//            Class classStudentServiceImpl = urlClassLoader.loadClass("com.chinasoft.wireless.wmp.algorithms.JavaCodeSum");
//            Method method = classStudentServiceImpl.getMethod("calculate");
//            Constructor localConstructor = classStudentServiceImpl.getConstructor(new Class[]{});
//            Object instance = localConstructor.newInstance(new Object[]{});
//            Object ret = method.invoke(instance);
//            System.out.println(ret);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            ClassLoaderUtil.releaseLoader(urlClassLoader);
//            if (reader != null) {
//                try {
//                    reader.close();
//                    urlClassLoader.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return null;
//    }


    @RequestMapping({"/get_localhost"})
    public Object getLocalhost(HttpServletRequest request) {
        String host = request.getRemoteHost();
        return host;
    }

    @RequestMapping({"/load_initial_information"})
    @WrapResult
    public Object loadInitialInformation(HttpServletRequest request) {
        Map<String, Object> inf = new LinkedHashMap<String, Object>(0);
        String host = request.getRemoteHost();
        inf.put("localhost", host);
        return inf;
    }


}
